// cd /projects/hsieh_project/proj_201809/code_2_201910/
// qstata --dofile=out_cind_con_d_city_emp_sales.do --statatype=mp --cpucount=5 &

set linesize 255
capture log close
if "`c(mode)'" == "" log using /projects/hsieh_project/proj_201809/code_2_201910/out_cind_con_d_city_emp_sales_log, replace text

clear all
capture noi program drop _all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/201910_main/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/201910_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/cind_con_d_city_emp_sales/"
capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

// Load rounding functions
do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_lpolyci.do"

//==============================================================================
// Changes in City

capture program drop read_city_d
program read_city_d
	args i_city v_sector c_mfg
	
	di "`i_city'"
	//local i_city = "msa"
	
	use "${dir_data}/cityind_sum_all_sel", clear
	
	merge 1:1 year1 year2 year city_code city sector ch_ind using ${dir_data}/sales_cityind_sum_sel, keepus(sales*)
	keep if city_code == "`i_city'"
	
	if "`c_mfg'" == "1" {
		keep if sector == 1
	}
	
	keep if year1 == ${year1} & year2 == ${year2}

	sort city ch_ind year
	
	gen c_est_cind = est_cind
	gen c_saless_ind_10 = sales_ind_10 / sales_cind
	gen c_saless_cind_t1 = sales_cind_t1 / sales_cind

	foreach i_var of var c_saless_ind_10 c_saless_cind_t1 c_est_cind {
		gen ln_`i_var' = ln(`i_var')
		by city ch_ind: gen ln_`i_var'_d = ln_`i_var'[2] - ln_`i_var'[1]
	}
	
	gen c_hhi = sales_hhi_cind
	gen c_hhin = sales_hhin_cind
	gen c_hhir = sales_hhir_cind
	foreach i_var of var c_hhi c_hhin c_hhir {
		by city ch_ind: gen `i_var'_d = `i_var'[2] - `i_var'[1]
		by city ch_ind: gen ln_`i_var'_d = ln(`i_var'[2]) - ln(`i_var'[1])
	}

	keep if year == 1977

	collapse (first) emp_city_${year1} (mean) ln_c_saless_* ln_c_est_cind_d *c_hhi_* *c_hhin_* *c_hhir_* [w=w_sva], by(city `v_sector') // Use new weight 200129

	foreach i_var of var emp_city_$year1 {
		gen ln_`i_var' = ln(`i_var')
	}
	
	*Round to 4sf in order to be in-line with disclosure requirement 
	foreach ivar2 of varlist ln_emp_city_${year1} c_hhi_d ln_c_saless_cind_t1_d ln_c_hhi_d ln_c_est_cind_d {
		sum `ivar2'
		rounding_4dig "`ivar2'"
		sum `ivar2'
	}
	
end

capture program drop fig_city_d_emp
program fig_city_d_emp
	args i_city i_var t_ytitle t_title t_subtitle f_weight i_sect
	/*
	twoway (scatter `i_var' ln_emp_city_${year1}, msize(small)), ///
		ytitle("`t_ytitle'") xtitle("ln(City Employment in ${year1})") ///
		title("`t_title'") subtitle("`t_subtitle'")
	graph export "${dir_outf}/scat_`i_var'_c_emp_${year1}_`i_city'`f_weight'.png", replace width(3000) height(2000)
	*/
	sum `i_var'
	sum ln_emp_city_${year1}
	
	lpolyci_data "" "lpoly_fig" "`i_var'" "ln_emp_city_${year1}" "99" "100"
	frame lpoly_fig {
		preserve
		keep `i_var'_x `i_var'_s `i_var'_se
		rounding_4dig "`i_var'_x `i_var'_s `i_var'_se"
		export excel ${dir_outf}/lpoly_cind_con_d_city_emp.xlsx, sheet("`i_city'_`i_var'") sheetreplace firstrow(var) keepcellfmt
		restore
		
		local v_y1 = "`i_var'"
		
		if "`i_city'"=="fips" {
			foreach i_suf in cil ciu s x {
				replace `v_y1'_`i_suf' = . if `v_y1'_x <5
				//replace `v_y2'_`i_suf' = . if `v_y2'_x <5
			}
			local c_xlab "xlab(5(2)15)"
		}
		else if inlist("`i_city'","msa","msa0") {
			foreach i_suf in cil ciu s x {
				replace `v_y1'_`i_suf' = . if `v_y1'_x <9
				//replace `v_y2'_`i_suf' = . if `v_y2'_x <9
			}
			local c_xlab "xlab(9(2)15)"
		}
		
		if "`i_city'" == "msa" {
			local t_city = "MSA"
			local t_cityc = "M0F*"
			local t_cityc2 = "M0F*"
		}
		if "`i_city'" == "fips" {
			local t_city = "FIPS"
			local t_cityc = "M*F0"
			local t_cityc2 = "M0F0"
		}
		if "`i_var'" == "ln_c_saless_cind_t1_d" local t_sample = "ALL, CINDT1*_`t_city'_CINDNEW_`t_cityc', CINDNEW_M*F*"
		if "`i_var'" == "c_hhi_d" local t_sample = "ALL, CINDNEW_`t_cityc2', CINDNEW_M*F*"
		if "`i_var'" == "ln_c_est_cind_d" local t_sample = "ALL, CINDNEW_`t_cityc2', CINDNEW_M*F*"
		
		twoway (rarea `v_y1'_cil `v_y1'_ciu `v_y1'_x, color(none) fi(inten0) lc(gs8) lw(thin)) ///
			(connected `v_y1'_s `v_y1'_x, m(i) lcolor(navy) lw(medthick)), ///
			legend(off) ///
			ytitle("`t_ytitle'") xtitle("ln(City Employment in ${year1})") ///
			title("`t_title'") subtitle("`t_subtitle', 99% CI") `c_xlab' note("Sample: `t_sample'")
		graph save "${dir_outf}/lpoly_`i_var'_c_emp_${year1}_`i_city'`f_weight'", replace
		if "`c(mode)'" != "batch" graph export "${dir_outf}/lpoly_`i_var'_c_emp_${year1}_`i_city'`f_weight'.png", replace width(3000) height(2500)
	}
end

capture program drop fig_group
program fig_group
	args i_city t_weight f_weight i_sect
	
	local t_city = upper("`i_city'")
	
	fig_city_d_emp "`i_city'" "ln_c_saless_cind_t1_d" "d ln(Sales Share of Top 1 City-Ind Firm)" "d ln(Sales Share of Top 1 City-Ind Firm)" "`t_city'`t_weight'" "`f_weight'" "`i_sect'"
	fig_city_d_emp "`i_city'" "c_hhi_d" "d HHI" "d HHI" "`t_city'`t_weight'" "`f_weight'" "`i_sect'"
	fig_city_d_emp "`i_city'" "ln_c_hhi_d" "d ln(HHI)" "d ln(HHI)" "`t_city'`t_weight'" "`f_weight'" "`i_sect'"
	fig_city_d_emp "`i_city'" "ln_c_est_cind_d" "d ln(Est)" "d ln(Est)" "`t_city'`t_weight'" "`f_weight'" "`i_sect'"
end


//==============================================================================
// Aggreate across all industries

global dir_outf "${dir_out}/cind_con_d_city_emp_sales/"
capture mkdir ${dir_outf}


/*
local i_city = "msa"
local t_city = upper("`i_city'")
read_city_d "`i_city'"
fig_group "msa" ", SV Weighted" "_sv" "0"

if 0 {
local i_city = "msa"
local t_city = upper("`i_city'")
read_city_d "`i_city'"
drop if city < 60
fig_group "msa0" ", SV Weighted" "_sv" "0"
}

local i_city = "fips"
local t_city = upper("`i_city'")
read_city_d "`i_city'"
fig_group "fips" ", SV Weighted" "_sv" "0"

local i_city = "czone"
local t_city = upper("`i_city'")
read_city_d "`i_city'"
fig_group "czone" ", SV Weighted" "_sv" "0"


*/
// MSA1983
local i_city = "msa1983"
local t_city = upper("`i_city'")
read_city_d "`i_city'"
fig_group "msa1983" ", SV Weighted" "_sv" "0"
/*For disclosure: can remove later */
keep c_hhi_d ln_emp_city_${year1}
save "/projects/disclosure/Apr2021/support/plots_data/section2/hhi_plot.dta", replace


/*
//------------------------------------------------------------------------------
// Improved geo

local i_city = "msa1983cz"
local t_city = upper("`i_city'")
read_city_d "`i_city'"
fig_group "msa1983cz" ", SV Weighted" "_sv" "0"

preserve
keep if city < 10000
fig_group "msa1983cz_m" ", SV Weighted" "_sv" "0"
restore
preserve
keep if city > 10000
fig_group "msa1983cz_c" ", SV Weighted" "_sv" "0"
restore


//==============================================================================
// Aggreate across MfG

if 0 {
global dir_outf "${dir_out}/cind_con_d_city_emp_sales_mfg/"
capture mkdir ${dir_outf}

local i_city = "msa"
local t_city = upper("`i_city'")
read_city_d "`i_city'" "" "1"
fig_group "msa" ", MfG, SV Weighted" "_sv_mfg"

drop if city < 60 & city != 40
local i_city = "msa"
local t_city = upper("`i_city'")
fig_group "msa0" ", MfG, SV Weighted" "_sv_mfg"

local i_city = "fips"
local t_city = upper("`i_city'")
read_city_d "`i_city'" "" "1"
fig_group "fips" ", MfG, SV Weighted" "_sv_mfg"
}


//==============================================================================
// Aggreate by sectors

if 0 {
global dir_outf "${dir_out}/cind_con_d_city_emp_sect_sales/"
capture mkdir ${dir_outf}

// MSA
global i_city = "msa"
global t_city = upper("${i_city}")
// SV weight
read_city_d "${i_city}" "sector"
levelsof sector, local(l_sect)
foreach i_sect in `l_sect' {
	preserve
	keep if sector == `i_sect'
	fig_group "msa" ", Sector `i_sect', SV Weighted" "_sect`i_sect'_sv"
	restore
}

// FIPS
global i_city = "fips"
global t_city = upper("${i_city}")
// SV weight
read_city_d "${i_city}" "sector"
levelsof sector, local(l_sect)
foreach i_sect in `l_sect' {
	preserve
	keep if sector == `i_sect'
	fig_group "fips" ", Sector `i_sect', SV Weighted" "_sect`i_sect'_sv"
	restore
}
}

di "Ended at $S_DATE $S_TIME"
capture noi log close
// End of do file
